import threading
from bs4 import BeautifulSoup
import requests
import time

url = 'https://www.lifeofpix.com/'
headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'}
try:
    req = requests.get(url,headers = headers)
except:
    print('获取失败')
#print(req.text)
soup = BeautifulSoup(req.text,'html.parser')
content_list = soup.find_all(class_ = 'actions')
#print(content)

def download(img_url,img_name):
    img = requests.get(img_url,headers = headers)
    with open('爬虫\\pics\\{}.jpg'.format(img_name),'wb') as f:
        f.write(img.content)
    time.sleep(2)

for content in content_list:
    img_url = content.find_all('a')[-1]['download']
    #print(img_url)
    name = content.div.ul.a['href'].split('/')[-2]
    #print(name)
    thread1 = threading.Thread(target=download,args=(img_url,name))
    thread1.start()




